<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tableData" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<el-table-column type="index" label="序号" width="60" align='center'>
				<template slot-scope="scope">   
                    {{pageSize*(nowPage-1)+scope.$index+1}}
                </template>
			</el-table-column>
			<el-table-column prop="productName" label="产品" min-width="200" align='center' >
			</el-table-column>
			<el-table-column label="状态" min-width="100" >
				<template slot-scope="scope">
					{{scope.row.useType==0?"上架":"下架"}}
				</template>
			</el-table-column>
			<el-table-column label="操作" width="250" align='center'  fixed="right" >
				<template slot-scope="scope">
					<el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button v-if="scope.row.useType==0" type="info" size="small" @click="offProduct(scope.row)">下架</el-button>
					<el-button v-else type="success" size="small" @click="onProduct(scope.row)">上架</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination background layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="text-align: center; ">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="editType" :visible.sync="editFormVisible" :close-on-click-modal="false" :before-close="closeDialog" :append-to-body="true" v-loading="editLoading">
			<el-form :model="productForm" label-width="120px" :rules="productRules" ref="productForm">
				<el-form-item label="产品名称" prop="productName">
					<el-input v-model="productForm.productName" placeholder="请输入产品名称，最长50个汉字" style="float:left;width:77%;" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="closeDialog">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { SysApi } from '@/api/index'

	export default {
		data() {
			return {
				filters: {
				},
				tableData: [],
				total: 0,
				listLoading: true,

				//产品信息表单
				productForm: {
					productId: '',
					productName: ''
				},

				editType: '',

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,

				productRules: { //产品信息表单验证
					productName:[
						{ required: true, message: '请填写产品名称', trigger: ['blur','change'] },
						{ min: 1, max: 50, message: '产品名称长度为 1 到 50 个字符', trigger: ['blur','change'] }
					]
            },
            pageSize:10,
            nowPage:1,

			}
		},
		created(){
			this.getProducts();

		},
		mounted() {
			this.getProducts();
		},
		methods: {

			handleCurrentChange(val) {
				this.nowPage = val;
				this.getProducts();
			},
			//获取产品列表
			getProducts() {
				let params = {
					pageNum: this.nowPage,
					pageSize: this.pageSize,
				};
				this.listLoading = true;

				SysApi.getALLProductListPage(params,res =>{
					this.total = res.total;
					this.tableData = res.list;
					this.listLoading = false;
				}, (errorType, error) => {
					this.listLoading = false;
				})


			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
	
					let params = {
						 productId: row.productId
						 };

					SysApi.deleteProduct(params,res =>{
						this.listLoading = false;
						if (res!=null)
						{
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						}
						this.getProducts();
					}, (errorType, error) => {
						this.listLoading = false;
					}).catch((error) => {
						this.listLoading = false;
					})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editType = "编辑";
				this.editLoading = true;
				this.productForm = Object.assign({}, row);
				this.editFormVisible = true;
				this.editLoading = false;
				
			},
			//显示新增界面
			handleAdd: function () {
				this.editType = "新增";
				this.editLoading = true;

				this.productForm.productId =  '';
				this.productForm.productName =  '';
				this.productForm.phaseId =  '';
				this.productForm.yearNum =  '';
				this.productForm.phaseName =  '';

				this.editFormVisible = true;
				this.editLoading = false;
		
			},
			//编辑
			editSubmit: function () {
				this.$refs.productForm.validate((valid) => {
					if (valid) {
							
							let params = Object.assign({}, this.productForm);
							
							SysApi.saveProduct(params,res =>{
								this.listLoading = false;
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['productForm'].resetFields();
								this.editFormVisible = false;
								this.getProducts();
							}, (errorType, error) => {
								this.$message({type: 'error', message: '提交失败!'})
								
	
							})
							
					}
				});
			},
			closeDialog: function () {
				
				this.$refs['productForm'].resetFields();
				this.editFormVisible = false;
			},
			onProduct(row){
				this.$confirm('是否上架产品?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					SysApi.onProduct({
						productId: row.productId
					}, res => {
						this.$message({type: 'success', message: '上架成功!'})
						this.getProducts();
					}, (errorType, error) => {
						this.$message({type: 'error', message: '上架失败!'})
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消'
					})
				})
			},
			offProduct(row) {
				this.$confirm('是否下架产品?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					SysApi.offProduct({
						productId: row.productId
					}, res => {
						this.$message({type: 'success', message: '下架成功!'})
						this.getProducts();
					}, (errorType, error) => {
						this.$message({type: 'error', message: '下架失败!'})
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消'
					})
				})
			}

		},
		
	}

</script>

<style>
.el-table >.el-table__fixed {
    height: 100% !important; 
  }
.el-table >.el-table__fixed-right {
    height: 100% !important; 
  }
.el-table--scrollable-x .el-table__body-wrapper {
    z-index: 1;
  }
</style>